<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="商品名称" prop="drivingLicense">
        <el-input
          v-model="queryParams.wgname"
          placeholder="请输商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>



      <el-form-item label="状态类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择状态类型" clearable>
          <el-option
            v-for="dict in delivery_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['product:status:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:status:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:status:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['product:status:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statusList" @selection-change="handleSelectionChange" @sort-change="changeTableSort">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" sortable="custom" v-if="columns[0].visible" prop="id" />

      <el-table-column label="商品名称" align="center" sortable="custom" v-if="columns[1].visible" prop="wgname" />
      <el-table-column label="里程" align="center" sortable="custom" v-if="columns[2].visible" prop="mileage" />
      <el-table-column label="油量" align="center" sortable="custom" v-if="columns[3].visible" prop="fuel" />
      <el-table-column label="图片" align="center" v-if="columns[4].visible" prop="images">
        <template #default="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="权重" align="center" sortable="custom" v-if="columns[5].visible" prop="weigh" />
      <el-table-column label="备注" align="center" sortable="custom" v-if="columns[6].visible" prop="remark" />
      <el-table-column label="状态类型" align="center" sortable="custom" v-if="columns[7].visible" prop="type">
        <template #default="scope">
          <dict-tag :options="delivery_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:status:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['product:status:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品状态对话框 -->
    <el-dialog :title="title" v-model="open" width="50vw" append-to-body :close-on-click-modal="false">
      <el-form ref="statusRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品名称" prop="goodsId">
          <inputsearch :changeValue="changeGoodsValue" :inputName="'id'" :inputValue="form.goodsId" :list="showGooodsList" />
        </el-form-item>
        <el-form-item label="随车工具包" prop="toolBag">
          <el-select v-model="form.toolBag" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头盔" prop="helmet">
          <el-select v-model="form.helmet" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机支架" prop="mobileHolder">
          <el-select v-model="form.mobileHolder" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行驶证" prop="drivingLicense">
          <el-select v-model="form.drivingLicense" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运动相机" prop="motionCamera">
          <el-select v-model="form.motionCamera" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="蓝牙通话机" prop="bluetoothPhone">
          <el-select v-model="form.bluetoothPhone" style="width: 100%;">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大灯" prop="headlamp">
          <el-select v-model="form.headlamp" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="刹车灯" prop="stoplight">
          <el-select v-model="form.stoplight" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转向灯" prop="corneringLamp">
          <el-select v-model="form.corneringLamp" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="左右后视镜" prop="leftRightMirrors">
          <el-select v-model="form.leftRightMirrors" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="后排照" prop="rearCamera">
          <el-select v-model="form.rearCamera" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="左侧面板" prop="leftPanel">
          <el-select v-model="form.leftPanel" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="右侧面板" prop="rightPanel">
          <el-select v-model="form.rightPanel" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="油箱" prop="tank">
          <el-select v-model="form.tank" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排气" prop="exhaust">
          <el-select v-model="form.exhaust" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="前轮毂以及轮胎" prop="frontWheelHubAndTire">
          <el-select v-model="form.frontWheelHubAndTire" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="后轮毂以及轮胎" prop="backWheelHubAndTire">
          <el-select v-model="form.backWheelHubAndTire" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发动机" prop="engine">
          <el-select v-model="form.engine" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="链条" prop="chain">
          <el-select v-model="form.chain" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="左把手" prop="leftHandle">
          <el-select v-model="form.leftHandle" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="右把手" prop="rightHandle">
          <el-select v-model="form.rightHandle" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="左护杠" prop="leftGuard">
          <el-select v-model="form.leftGuard" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="右护杠" prop="rightGuard">
          <el-select v-model="form.rightGuard" style="width: 100%;">
            <el-option
              v-for="dict in vehicle_acceptance_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="里程" prop="mileage">
          <el-input v-model="form.mileage" type="number" placeholder="请输入里程" />
        </el-form-item>
        <el-form-item label="油量" prop="fuel">
          <el-input v-model="form.fuel" type="number" placeholder="请输入油量" />
        </el-form-item>
        <el-form-item label="状态类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择状态类型" style="width: 100%;">
            <el-option
              v-for="dict in delivery_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <image-upload v-model="form.images" :isShowTip="false"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Status">
import { listStatus, getStatus, delStatus, addStatus, updateStatus } from "@/api/product/status";
import { listsGoods } from "@/api/product/goods";
import inputsearch from "@/components/inputsearch";

const { proxy } = getCurrentInstance();
const { delivery_type, vehicle_acceptance_status } = proxy.useDict('delivery_type', 'vehicle_acceptance_status');

const columns = ref([
  { key: 0, label: `ID`,prop: 'id', visible: true },
  { key: 1, label: `商品名称`,prop: 'wgname', visible: true },
  { key: 2, label: `里程`,prop: 'mileage', visible: true },
  { key: 3, label: `油量`,prop: 'fuel', visible: true },
  { key: 4, label: `图片`,prop: 'images', visible: true },
  { key: 5, label: `权重`,prop: 'weigh', visible: true },
  { key: 6, label: `备注`,prop: 'remark', visible: true },
  { key: 7, label: `状态类型`,prop: 'type', visible: true }
]);
const statusList = ref([]);
const showGooodsList = ref([]);                         // 商品列表
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    weigh: null,
    goodsId: '',
    orderId: null,
    images: null,
    toolBag: 0,
    helmet: 0,
    mobileHolder: 0,
    drivingLicense: null,
    motionCamera: 0,
    bluetoothPhone: 0,
    headlamp: 0,
    stoplight: 0,
    corneringLamp: 0,
    leftRightMirrors: 0,
    rearCamera: 0,
    leftPanel: 0,
    rightPanel: 0,
    tank: 0,
    exhaust: 0,
    frontWheelHubAndTire: 0,
    backWheelHubAndTire: 0,
    engine: 0,
    chain: 0,
    leftHandle: 0,
    rightHandle: 0,
    leftGuard: 0,
    rightGuard: 0,
    mileage: null,
    fuel: null,
    type: null,
    orderByColumn: null,
    isAsc: null
  },
  rules: {
    goodsId: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    toolBag: [
      { required: true, message: "随车工具包:0=有,1=无不能为空", trigger: "blur" }
    ],
    helmet: [
      { required: true, message: "头盔:0=有,1=无不能为空", trigger: "blur" }
    ],
    mobileHolder: [
      { required: true, message: "手机支架:0=有,1=无不能为空", trigger: "blur" }
    ],
    drivingLicense: [
      { required: true, message: "行驶证:0=有,1=无不能为空", trigger: "blur" }
    ],
    motionCamera: [
      { required: true, message: "运动相机:0=有,1=无不能为空", trigger: "blur" }
    ],
    bluetoothPhone: [
      { required: true, message: "蓝牙通话机:0=有,1=无不能为空", trigger: "blur" }
    ],
    headlamp: [
      { required: true, message: "大灯:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    stoplight: [
      { required: true, message: "刹车灯:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    corneringLamp: [
      { required: true, message: "转向灯:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    leftRightMirrors: [
      { required: true, message: "左右后视镜:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    rearCamera: [
      { required: true, message: "后排照:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    leftPanel: [
      { required: true, message: "左侧面板:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    rightPanel: [
      { required: true, message: "右侧面板:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    tank: [
      { required: true, message: "油箱:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    exhaust: [
      { required: true, message: "排气:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    frontWheelHubAndTire: [
      { required: true, message: "前轮毂以及轮胎:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    backWheelHubAndTire: [
      { required: true, message: "后轮毂以及轮胎:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    engine: [
      { required: true, message: "发动机:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    chain: [
      { required: true, message: "链条:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    leftHandle: [
      { required: true, message: "左把手:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    rightHandle: [
      { required: true, message: "右把手:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    leftGuard: [
      { required: true, message: "左护杠:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    rightGuard: [
      { required: true, message: "右护杠:0=轻微划痕,1=中度划痕,2=重度划痕,3=掉漆,4=变形,5=凹陷,6=破损,7=异响不能为空", trigger: "blur" }
    ],
    mileage: [
      { required: true, message: "里程不能为空", trigger: "blur" }
    ],
    fuel: [
      { required: true, message: "油量不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "状态类型:0=交车,1=还车不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品状态列表 */
function getList() {
  loading.value = true;
  listStatus(queryParams.value).then(response => {
    statusList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询商品列表 */
function getGoodsList() {
  listsGoods({
    pageNum: 1,
    pageSize: 10000
  }).then(response => {
    showGooodsList.value = response.rows.map(e => {
      e.value = e.name + ('('+ e.desc +')')
      return e
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    weigh: null,
    goodsId: '',
    orderId: null,
    images: null,
    remark: null,
    toolBag: 0,
    helmet: 0,
    mobileHolder: 0,
    drivingLicense: null,
    motionCamera: 0,
    bluetoothPhone: 0,
    headlamp: 0,
    stoplight: 0,
    corneringLamp: 0,
    leftRightMirrors: 0,
    rearCamera: 0,
    leftPanel: 0,
    rightPanel: 0,
    tank: 0,
    exhaust: 0,
    frontWheelHubAndTire: 0,
    backWheelHubAndTire: 0,
    engine: 0,
    chain: 0,
    leftHandle: 0,
    rightHandle: 0,
    leftGuard: 0,
    rightGuard: 0,
    mileage: null,
    fuel: null,
    createTime: null,
    updateTime: null,
    type: null
  };
  proxy.resetForm("statusRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.orderByColumn = null
  queryParams.value.isAsc = null
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStatus(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["statusRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStatus(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStatus(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除商品状态编号为"' + _ids + '"的数据项？').then(function() {
    return delStatus(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('product/status/export', {
    ...queryParams.value
  }, `status_${new Date().getTime()}.xlsx`)
}

// 改变商品值
const changeGoodsValue = (request) => {
  form.value.goodsId = request.id
}

// 改变排序
function changeTableSort(e){
  if(e.order){
    queryParams.value.orderByColumn = e.prop
    queryParams.value.isAsc = e.order == 'descending' ? 'desc' : 'asc'
  }else{
    queryParams.value.orderByColumn = null
    queryParams.value.isAsc = null
  }
  handleQuery()
}

getList();
getGoodsList();
</script>
